package com.urms.utils;

import io.jsonwebtoken.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class JWTUtils {
    private static final String jwtToken = "123456fsy!@#$%";

    public static String createToken(int id) {
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",id);
        JwtBuilder jwtBuilder = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256,jwtToken)
                .addClaims(claims)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + 30L * 24 * 60 * 60 * 1000));
        return jwtBuilder.compact();
    }


    public static Map<String,Object> checkToken(String token){
        try {
            Jwt parse = Jwts.parser().setSigningKey(jwtToken).parse(token);
            return (Map<String, Object>) parse.getBody();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

//    public static void main(String[] args) {
//        String token = JWTUtils.createToken(1);
//        log.info("token:{}",token);
//        Map<String, Object> map = JWTUtils.checkToken(token);
//        log.info("map:{}",map.get("id"));
//    }
}
